package com.css.fxfzaqbz.modules.base.repository.impl;

import com.css.fxfzaqbz.modules.base.entity.BuildingVulnerability;
import com.css.fxfzaqbz.modules.base.entity.CountProcess;
import com.css.fxfzaqbz.modules.base.entity.District;
import com.css.fxfzaqbz.modules.base.entity.HouseDestoryEntity;
import com.css.fxfzaqbz.modules.base.repository.CountProcessRepository;
import com.css.fxfzaqbz.modules.data.entity.Site;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class CountProcessRepositoryImpl implements CountProcessRepository {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(CountProcess countProcess) {
        String sql="insert into sys_count_process values(:id, :province, :city, :prefecture, :structureType, :intensity, :one, :two, " +
                ":three, :four, :five, :ratio)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(countProcess);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<CountProcess> query(CountProcess countProcess) {
        StringBuilder sql = new StringBuilder("select * from sys_count_process");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(countProcess.getProvince())){
            whereSql.append(" and province = '"+countProcess.getProvince()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(countProcess.getCity())){
            whereSql.append(" and city = '"+countProcess.getCity()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(countProcess.getPrefecture())){
            whereSql.append(" and prefecture = '"+countProcess.getPrefecture()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(countProcess.getIntensity())){
            whereSql.append(" and intensity = '"+countProcess.getIntensity()+"'");
        }
        List<CountProcess> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CountProcess.class));
        return list;
    }

    @Override
    public void delete() {
        String sql = "DELETE FROM sys_count_process";
        jdbcTemplate.update(sql);
    }

    @Override
    public void saveHouseDestory(List<HouseDestoryEntity> houseDestoryEntities) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(houseDestoryEntities.toArray());
        String sql="insert into sys_house_destory values(:id, :province, :city, :county, :jglx, :mqsfld, :maxDestoryType, :iaId,ST_GeometryFromText(:geom,4490),:jzmc,:buildTime,:longitude,:latitude,:resultAnalysis,:number)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);

    }

    @Override
    public void deleteHouseDestory(District district, String iaId) {
        String sql="delete from sys_house_destory where province='"+district.getProvince()+"' and city='"+district.getCity()+"' and county='"+district.getPrefecture()+"' and ia_id='"+iaId+"' ";
        // String sql="delete from sys_house_destory ";
        jdbcTemplate.update(sql);
    }

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String sdaId) {
        String sql="select jzmc,longitude,latitude,jglx,build_time,mqsfld,max_destory_type from sys_house_destory where result_analysis ='"+sdaId+"'";
        Integer total = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouseDestoryEntity.class)).size();
        int firstIndex=(curPage-1) * pageSize;
        int lastIndex=pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        List<HouseDestoryEntity> houseDestoryEntities = jdbcTemplate.query(sql+limitSql, new BeanPropertyRowMapper<>(HouseDestoryEntity.class));
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",houseDestoryEntities);
        return map;
    }

    @Override
    public List<HouseDestoryEntity> statistics(String sdaId) {
        String sql="select jzmc,longitude,latitude,jglx,build_time,mqsfld,max_destory_type from sys_house_destory where result_analysis ='"+sdaId+"'";
        List<HouseDestoryEntity> houseDestoryEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouseDestoryEntity.class));
        return houseDestoryEntities;
    }

    @Override
    public void batchSave(List<CountProcess> arrayList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(arrayList.toArray());
        String sql="insert into sys_count_process values(:id, :province, :city, :prefecture, :structureType, :intensity, :one, :two, " +
                ":three, :four, :five, :ratio)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }
}
